package com.sky.loveshop.controller;

import com.sky.loveshop.entity.Result;
import com.sky.loveshop.entity.Shelf;
import com.sky.loveshop.entity.common.PageResult;
import com.sky.loveshop.entity.exception.BizException;
import com.sky.loveshop.entity.exception.BizExceptionEnum;
import com.sky.loveshop.entity.qo.ShelfQO;
import com.sky.loveshop.entity.vo.AreaShelfVO;
import com.sky.loveshop.entity.vo.ShelfVO;
import com.sky.loveshop.entity.vo.SkuDetailVO;
import com.sky.loveshop.service.ShelfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author xcy
 * @version V1.0
 * @description
 * @date 2024/8/18 0:04
 */
@RestController
@RequestMapping("/shelf")
public class ShelfController {

    @Autowired
    private ShelfService shelfService;

    @PostMapping("/listShelfPages")
    public Result<PageResult<ShelfVO>> listShelfPages(@RequestBody ShelfQO shelfQO) {
        return Result.page(shelfService.listShelfPages(shelfQO));
    }

    @PostMapping("/listShelf")
    public Result<List<ShelfVO>> listShelf(@RequestBody ShelfQO shelfQO) {
        return Result.success(shelfService.listShelf(shelfQO));
    }

    @PutMapping("/addShelf")
    public Result<Integer> addShelf(@RequestBody @Valid Shelf shelf) {
        return Result.success(shelfService.addShelf(shelf));
    }

    @DeleteMapping("/delShelfById")
    public Result<Integer> delShelfById(Integer id) throws BizException {
        if (id == null) {
            throw new BizException(BizExceptionEnum.ID_MUST_NEED);
        }
        return Result.success(shelfService.delShelfById(id));
    }

    @PostMapping("/editShelf")
    public Result<Integer> editShelf(@RequestBody Shelf shelf) throws BizException {
        if (shelf.getId() == null) {
            throw new BizException(BizExceptionEnum.ID_MUST_NEED);
        }
        return Result.success(shelfService.updateShelfById(shelf));
    }

    @GetMapping("/getShelfById")
    public Result<Shelf> getShelfById(Integer id) throws BizException {
        if (id == null) {
            throw new BizException(BizExceptionEnum.ID_MUST_NEED);
        }
        return Result.success(shelfService.getShelfById(id));
    }

    /**
     * 根据商店id获取所有的区域(每个区域下层数以及总商品位置)
     **/
    @GetMapping("/getShelfListByShoId")
    public Result<List<AreaShelfVO>> getShelfListByShopId(Integer shopId) {
        return Result.success(shelfService.getShelfListByShopId(shopId));
    }

    /**
     * 根据商店id和区域名称 获取所有层数以及位置
     * @param shopId 我方系统商店id
     * @param area 区域 A B C D ......
     * @return 每一层的商品sku信息
     * @throws Exception
     */
    @GetMapping("/getIndexByShoIdAndArea")
    public Result<List<SkuDetailVO>> getIndexByShoIdAndArea(@RequestParam Integer shopId,
                                                            @RequestParam String area,
                                                            @RequestParam(defaultValue = "") String keyWords) throws Exception {
        return Result.success(shelfService.getIndexByShoIdAndArea(shopId, area, keyWords));
    }

}
